package io.github.yotta.dict.autoconfiguration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import io.github.yotta.dict.dao.entity.SysDictDao;
import io.github.yotta.dict.dao.mybatisplus.impl.MybatisPlusSysDictDao;
import io.github.yotta.dict.dao.mybatisplus.impl.SysDictMybatisService;
import io.github.yotta.dict.dao.mybatisplus.impl.SysDictMybatisServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(MybatisPlusAutoConfiguration.class)
public class SysDictMybatisPlusAutoConfiguration {


    @ConditionalOnClass(SysDictMybatisServiceImpl.class)
    @MapperScan("io.github.yotta.dict.dao.mybatisplus.mapper")
    @Configuration
    static class MapperScanConfiguration {

    }

    @Bean
    @ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public SysDictMybatisService sysDictMybatisService() {
        return new SysDictMybatisServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(SysDictDao.class)
    public SysDictDao sysDictDao(SysDictMybatisService sysDictMybatisService) {
        return new MybatisPlusSysDictDao(sysDictMybatisService);
    }

}